package com.github.szhittech.service.prop;

import com.github.szhittech.bean.LibrariesBean;
import com.github.szhittech.bean.LibraryBean;
import com.github.szhittech.service.api.http.SimpleHttpUtils;
import com.github.szhittech.service.ext.LibraryCompile;
import com.github.szhittech.util.WordUtil;
import com.google.gson.Gson;

import org.gradle.api.Project;
import org.gradle.api.plugins.ExtraPropertiesExtension;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 解析更目录gradle.properties键值对
 */
public class PropertyApi {
    public static void init(Project project) {
        decode(project);
        //加载服务器文件，用于对比本地依赖库版本
        loadServerConfig(project);
        project.getExtensions().add("libs", new LibraryCompile(project.getRootProject(), Properties.SERVER_LIBS));
    }

    public static void loadServerConfig(Project project) {
        String filepath = Properties.LIBS_JSON_URI;
        if (filepath.startsWith("http")) {
            Properties.SERVER_LIBS = getServerJson(filepath, LibrariesBean.class);
            System.err.println(WordUtil.STRING_4 + filepath);
            if (Properties.SERVER_LIBS == null) {
                throw new NullPointerException("get server json null");
            }
        } else {
            File file = new File(project.getRootDir(), filepath);
            if (!file.exists()) {
                throw new IllegalStateException(WordUtil.STRING_5 + file.getAbsolutePath() + WordUtil.STRING_6 + file.getAbsolutePath());
            }
            try {
                Properties.SERVER_LIBS = LibrariesBean.fromFile(file);
                System.err.println(WordUtil.STRING_7 + file.getCanonicalPath());
                if (Properties.SERVER_LIBS == null) {
                    throw new NullPointerException(WordUtil.STRING_8);
                }
            } catch (IOException e) {
                e.printStackTrace();
                throw new IllegalStateException(WordUtil.STRING_6 + file.getAbsolutePath() + WordUtil.STRING_9 + e.getMessage());
            }

        }

        if (Properties.SERVER_LIBS != null) {
            List<LibraryBean> libs = Properties.SERVER_LIBS.getLibs();
            if (libs != null && libs.size() > 0) {
                for (LibraryBean item : libs) {
                    String groupName = item.getGroup() + ":" + item.getName();
                    Properties.SERVER_LIBS_MAP.put(groupName, item);
                }
            }
            //System.err.println("From Server:" + msg);
        }
    }

    public static void decode(Project project) {
        ExtraPropertiesExtension extraProperties = project.getExtensions().getExtraProperties();
        if (extraProperties.has("clife.libs.uri")) {
            Object obj2 = extraProperties.get("clife.libs.uri");
            if (obj2 != null && obj2 instanceof String) {
                Properties.LIBS_JSON_URI = (String) obj2;
            }
        }

        if (extraProperties.has("clife.conf.uri")) {
            Object obj2 = extraProperties.get("clife.libs.uri");
            if (obj2 != null && obj2 instanceof String) {
                Properties.CONF_JSON_URI = (String) obj2;
            }
        }

        if (extraProperties.has("clife.svn.username")) {
            Object obj2 = extraProperties.get("clife.svn.username");
            if (obj2 != null && obj2 instanceof String) {
                Properties.SVNUSERNME = (String) obj2;
            }
        }

        if (extraProperties.has("clife.svn.password")) {
            Object obj2 = extraProperties.get("clife.svn.password");
            if (obj2 != null && obj2 instanceof String) {
                Properties.SVNPASSWORD = (String) obj2;
            }
        }

        if (extraProperties.has("clife.release")) {
            Object obj2 = extraProperties.get("clife.release");
            if (obj2 != null) {
                String value = obj2.toString();
                Properties.ISRELEASE = value.equalsIgnoreCase("true") ? true : false;
            }
        }

    }

    private static <T> T getServerJson(String url, Class<T> cls) {
        if (url == null || url.equalsIgnoreCase(""))
            throw new IllegalArgumentException(WordUtil.STRING_10);
        try {
            Map<String, String> headers = new HashMap<>();
            headers.put("username", Properties.SVNUSERNME);
            headers.put("password", Properties.SVNPASSWORD);
            String msg = SimpleHttpUtils.get(url, headers);
            //System.err.println(">>>>>>>>>>>>>>>>>>>>>" + msg);
            return new Gson().fromJson(msg, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
